package com.crm.controller;

import com.crm.DTO.BusinessAssignDto;
import com.crm.DTO.BusinessDto;
import com.crm.DTO.BusinessFollowDto;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.ResultVO;
import com.crm.common.result.TableDataInfo;
import com.crm.entity.Business;
import com.crm.service.BusinessService;
import com.crm.vo.BusinessFollowVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

import static com.crm.common.result.AjaxResult.error;

/**
 * 2025/9/5 10:20
 * 注释：
 */

@RestController
@RequestMapping("/business")
@Tag(name = "商机管理接口")
@Slf4j
public class BusinessController {


    @Autowired
    private BusinessService businessService;

    /**
     * 商机列表 (分页)
     *
     * @param businessDto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<Business> listPage(BusinessDto businessDto,
                                            @RequestParam(value = "params[beginCreateTime]", required = false) LocalDate beginCreateTime,
                                            @RequestParam(value = "params[endCreateTime]", required = false) LocalDate endCreateTime) {
        log.info("商机列表(分页查询){}", businessDto);
        if (beginCreateTime != null && endCreateTime != null) {
            businessDto.setBeginCreateTime(beginCreateTime.atStartOfDay());
            businessDto.setEndCreateTime(endCreateTime.atStartOfDay());
        }
        return businessService.searchBusiness(businessDto);
    }

    /**
     * 查看商机详情
     */
    @GetMapping("/{id}")
    public ResultVO<Business> getBusiness(@PathVariable Long id) {
        log.info("查看商机详情");

        Business business = businessService.getById(id);
        return ResultVO.success(business);
    }

    /**
     * 查询公海池(分页)
     */
    @GetMapping("/pool")
    public TableDataInfo<Business> listPool(BusinessDto businessDto,
                                            @RequestParam(value = "params[beginCreateTime]", required = false) LocalDate beginCreateTime,
                                            @RequestParam(value = "params[endCreateTime]", required = false) LocalDate endCreateTime) {
        log.info("公海池(分页){}", businessDto);
        if (beginCreateTime != null && endCreateTime != null) {
            businessDto.setBeginCreateTime(beginCreateTime.atStartOfDay());
            businessDto.setEndCreateTime(endCreateTime.atStartOfDay());
        }
        return businessService.getBusiness(businessDto);
    }

    /**
     * 新增商机
     */
    @PostMapping
    public ResultVO saveBusiness(@RequestBody Business business) {
        log.info("新增商机{}", business);
        boolean b = businessService.saveBusiness(business);
        return ResultVO.success(b);
    }


    /**
     * 商机详情(历史沟通记录)
     */
    @GetMapping("/record/list")
    public ResultVO getBusinessRecord(Long businessId) {
        List<BusinessFollowVo> list = businessService.getBusinessFollowList(businessId);
        return ResultVO.success(list);
    }


    /**
     * 商机跟进(跟进记录、更新商机信息)
     */
    @PostMapping("/record")
    public ResultVO addBusinessRecord(@RequestBody BusinessFollowDto dto) {
        log.info("商机跟进{}", dto);
        businessService.addBusinessRecord(dto);
        return ResultVO.success();
    }

    /**
     * 分配商机
     */
    @PutMapping("/assignment")
    public ResultVO AllocationBusiness(@RequestBody BusinessAssignDto businessAssignDto) {
        log.info("分配商机{}", businessAssignDto);
        businessService.AllocationBusiness(businessAssignDto);
        return ResultVO.success();

    }


}
